package com.mwm.loyal;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mwm.loyal.bean.ResultBean;
import com.mwm.loyal.bean.ResultMap;
import com.mwm.loyal.util.GsonUtil;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
       /* String resources = "mybatis-config.xml";
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resources);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession = factory.openSession();
            AccountBean bean = sqlSession.selectOne("getUserByAccount", "1");
            sqlSession.commit();
            System.out.println(bean.toString());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("catch:" + e.toString());
        } finally {
            StreamUtil.close(reader);
        }*/
        //System.out.println(TimeUtil.getDate(new Date(), IContactImpl.Str.TIME_ALL));
         gsont();
        // hex();
        //patch();
    }

    private static void patch() {
        Pattern pattern = Pattern.compile("^[0-9]$");
        for (int i = 0; i < 256; i++) {
            String string = Integer.toHexString(i);
            boolean match = pattern.matcher(string).matches();
            System.out.println("数字"+i+"转16进制["+string+"]-是否匹配："+match);
        }
        boolean match = pattern.matcher("").matches();
        System.out.println("数字"+"转16进制["+"]-是否匹配："+match);
    }

    private static void hex() {
        String s = Integer.toHexString(128);
        System.out.println(s);
        int ss = Integer.parseInt("011");
        System.out.println(ss);
        s = Integer.toHexString(ss);
        System.out.println(s);

        String hex = "b";
        Integer x = Integer.parseInt(hex, 16);
        System.out.println(x);
    }

    private static void gsont() {
        String path = "E:\\IntelliJ Space\\MwmServer\\target\\MwMServer\\";
        System.out.println(path.substring(0, path.lastIndexOf("target")));
        ResultMap resultMap = ResultMap.getInstance();
        String json = resultMap.set("1", "登录成功").setObj("10086").setList(Arrays.asList(1.001f, 2.002f, 3.003f)).toJson();
        System.out.println("objJson->" + json);
        try {
            ResultBean<String, List<Float>> bean = jsonTest(json, String.class, Float.class);
            System.out.println("obj->" + bean.toString());
            String tJson = GsonUtil.obj2Json(bean);
            System.out.println("tJson->" + tJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        json = resultMap.set("1", "加载成功").setList(Arrays.asList(1.134, 8.675)).setObj(10086).toJson();
        System.out.println("listJson->" + json);
        try {
            ResultBean<String, List<Double>> bean2 = jsonTest(json, String.class, Double.class);
            System.out.println("list->" + bean2.toString());
            String tJson = GsonUtil.obj2Json(bean2);
            System.out.println("t2Json->" + tJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <O, A> ResultBean<O, List<A>> jsonTest(String json, Class<O> objClass, Class<A> aClass) {
        if (null == json)
            return null;
        //return gson.fromJson(json, typeBuilder.build());
        TypeToken<ResultBean<O, List<A>>> typeToken = (TypeToken<ResultBean<O, List<A>>>) TypeToken.getParameterized(ResultBean.class, objClass, aClass);
        return new Gson().fromJson(json, typeToken.getType());
    }
}
